const DatavChartsBuild = {
  getTypeValue: (value, dbType) => {
    try {
      if (value && dbType === 'date' && typeof value === 'number') {
        value = new Date(value).format('yyyy-MM-dd')
      }
    } catch (e) {}
    return value
  },
  /**
   * 构建图表属性数据
   */
  buildChart: (field, data) => {
    let options = null
    switch (field.alias) {
      case 'line':
        options = DatavChartsBuild.buildLine(field, data)
        break
      case 'bar':
        options = DatavChartsBuild.buildLine(field, data)
        break
      case 'pie':
        options = DatavChartsBuild.buildPie(field, data)
        break
      case 'radar':
        options = DatavChartsBuild.buildRadar(field, data)
        break
      case 'gauge':
        options = DatavChartsBuild.buildGauge(field, data)
        break
      case 'ring-chart':
      case 'capsule-chart':
      case 'conical-column-chart':
      case 'scroll-ranking-board':
        options = DatavChartsBuild.buildRingChart(field, data)
        break
      case 'water-level-pond':
        options = DatavChartsBuild.buildLevelPondChart(field, data)
        break
      case 'percent-pond':
        options = DatavChartsBuild.buildPondChart(field, data)
        break
      case 'digital-flop':
        options = DatavChartsBuild.buildDigitalFlop(field, data)
        break
      case 'scroll-board':
        options = DatavChartsBuild.buildScrollBoard(field, data)
        break
      case 'decoration7':
      case 'decoration9':
      case 'decoration11':
        options = DatavChartsBuild.buildDecoration(field, data)
        break
    }
    return options
  },
  buildLine: (field, data) => {
    let options = field.options
    let chartType = field.alias
    if (options.yField && options.yField.length > 0 && options.xField.alias) {
      let legendData = {data: [], textStyle: {color: '#fff'}}
      let xAxisData = []
      let seriesData = []
      options.yField.forEach(item => {
        legendData.data.push(item.desc)
        seriesData.push({
          alias: item.alias.toUpperCase(),
          dbType: item.dbType,
          name: item.desc,
          type: chartType,
          data: []
        })
      })
      if (legendData.data.length > 1) {
        options.option.legend = legendData
      }
      if (data && data.length > 0) {
        for (let i = 0; i < data.length; i++) {
          let row = data[i]

          for (const key in row) {
            row[key.toUpperCase()] = row[key]
          }

          let value = row[options.xField.alias.toUpperCase()]
          xAxisData.push(value)
          seriesData.forEach(series => {
            const value = DatavChartsBuild.getTypeValue(
              row[series.alias],
              series.dbType
            )
            let nValue = Number(value)
            if (isNaN(nValue)) {
              nValue = value.length
            }
            series.data.push(nValue)
          })
        }
      }
      options.option.xAxis.data = xAxisData
      seriesData.forEach(series => {
        delete series.alias
        delete series.dbType
      })
      options.option.series = seriesData
      options.option.tooltip = {
        trigger: 'axis'
      }
      if (data.length > 20 && options.option.series.dataZoom) {
        options.option.dataZoom = [
          {
            start: 0,
            end: 10,
            handleIcon:
              'M10.7,11.9v-1.3H9.3v1.3c-4.9,0.3-8.8,4.4-8.8,9.4c0,5,3.9,9.1,8.8,9.4v1.3h1.3v-1.3c4.9-0.3,8.8-4.4,8.8-9.4C19.5,16.3,15.6,12.2,10.7,11.9z M13.3,24.4H6.7V23h6.6V24.4z M13.3,19.6H6.7v-1.4h6.6V19.6z',
            handleSize: '80%',
            handleStyle: {
              color: '#fff',
              shadowBlur: 3,
              shadowColor: 'rgba(0, 0, 0, 0.6)',
              shadowOffsetX: 2,
              shadowOffsetY: 2
            }
          }
        ]
        options.option.grid.bottom = 50
      } else {
        //option.grid.bottom = "3%";
        delete options.option.dataZoom
      }
      if (options.tipFormatter) {
        options.option.tooltip.formatter = options.tipFormatter
      }
      if (options.option.title && !options.option.title.text) {
        options.option.title.subtext = ''
      }
      return options
    }
    return null
  },
  buildPie: (field, data) => {
    let options = field.options
    if (options.yField.alias && options.xField.alias) {
      let legendData = {
        orient: 'vertical',
        left: 'left',
        textStyle: {color: '#fff'},
        type: 'scroll'
      }
      let seriesData = {
        name: options.xField.desc,
        type: 'pie',
        radius: '50%',
        data: []
      }
      let legendDataName = []
      if (data && data.length > 0) {
        for (let i = 0; i < data.length; i++) {
          let row = data[i]
          for (const key in row) {
            row[key.toUpperCase()] = row[key]
          }
          const value = DatavChartsBuild.getTypeValue(
            row[options.yField.alias.toUpperCase()],
            options.yField.dbType
          )
          //todo 饼状图value 只支持数字
          let nValue = Number(value)
          if (isNaN(nValue)) {
            if (value) {
              nValue = value.length
            }
            nValue = 0
          }
          legendDataName.push(row[options.xField.alias.toUpperCase()])
          seriesData.data.push({
            name: row[options.xField.alias.toUpperCase()],
            value: nValue
          })
        }
      }
      legendData.data = legendDataName
      options.option.legend = legendData
      seriesData.itemStyle = {
        emphasis: {
          shadowBlur: 10,
          shadowOffsetX: 0,
          shadowColor: 'rgba(0, 0, 0, 0.5)'
        }
      }
      seriesData.emphasis = {
        itemStyle: {
          shadowBlur: 10,
          shadowOffsetX: 0,
          shadowColor: 'rgba(0, 0, 0, 0.5)'
        }
      }
      if (options.tipFormatter) {
        options.option.tooltip.formatter = options.tipFormatter
      }
      options.option.series = seriesData
      return options
    }
    return null
  },
  buildRadar: (field, data) => {
    let options = field.options
    if (options.yField && options.yField.length > 0 && options.xField.alias) {
      let seriesData = []
      let legendData = {data: [], textStyle: {color: '#fff'}}
      let radarData = {indicator: []}
      options.yField.forEach(item => {
        radarData.indicator.push({name: item.desc})
      })
      if (data && data.length > 0) {
        for (let i = 0; i < data.length; i++) {
          let row = data[i]
          let seriesItem = {name: row[options.xField.alias], value: []}
          legendData.data.push(row[options.xField.alias])
          options.yField.forEach(item => {
            const value = DatavChartsBuild.getTypeValue(
              row[item.alias.toUpperCase()],
              item.dbType
            )
            seriesItem.value.push(value)
          })
          seriesData.push(seriesItem)
        }
      }
      options.option.radar = radarData
      options.option.legend = legendData
      options.option.series = {name: '', type: 'radar', data: seriesData}
      if (options.tipFormatter) {
        options.option.tooltip.formatter = options.tipFormatter
      }
      return options
    }
    return null
  },
  buildGauge: (field, data) => {
    let options = field.options
    if (options.yField && options.yField.length > 0) {
      let seriesData = []
      options.yField.forEach(item => {
        seriesData.push({
          alias: item.alias.toUpperCase(),
          dbType: item.dbType,
          type: 'gauge',
          name: item.desc,
          detail: {formatter: '{value}'},
          data: []
        })
      })
      if (data && data.length > 0) {
        //	    	for (var i = 0; i < data.length; i++) {
        let row = data[0]
        seriesData.forEach(item => {
          const value = DatavChartsBuild.getTypeValue(
            row[item.alias],
            item.dbType
          )
          item.data.push({value: value, name: item.name})
        })
        //			}
      }
      if (options.tipFormatter) {
        options.option.tooltip.formatter = options.tipFormatter
      }
      options.option.series = seriesData
      return options
    }
    return null
  },
  buildRingChart: (field, data) => {
    let options = field.options
    if (
      options.xField &&
      options.xField.alias &&
      options.yField &&
      options.yField.alias
    ) {
      options.config.data = []
      if (data && data.length > 0) {
        for (var i = 0; i < data.length; i++) {
          let row = data[i]
          const value = DatavChartsBuild.getTypeValue(
            row[options.yField.alias],
            options.yField.dbType
          )
          options.config.data.push({
            value: value,
            name: row[options.xField.alias]
          })
        }
      }
      return options
    }
    return null
  },
  buildLevelPondChart: (field, data) => {
    let options = field.options
    if (options.yField && options.yField.alias) {
      options.config.data = []
      if (data && data.length > 0) {
        data.forEach(row => {
          const value = DatavChartsBuild.getTypeValue(
            row[options.yField.alias],
            options.yField.dbType
          )
          options.config.data.push(value)
        })
        //                let row = data[0];
        //                const value = DatavChartsBuild.getTypeValue(row[options.yField.alias], options.yField.dbType);
        //                options.config.data.push(value);
      }
      return options
    }
    return null
  },
  buildPondChart: (field, data) => {
    let options = field.options
    if (options.yField && options.yField.alias) {
      if (data && data.length > 0) {
        let row = data[0]
        const value = DatavChartsBuild.getTypeValue(
          row[options.yField.alias],
          options.yField.dbType
        )
        options.config.value = value
      }
      return options
    }
    return null
  },
  buildDigitalFlop: (field, data) => {
    let options = field.options
    if (options.yField && options.yField.alias) {
      options.config.number = []
      if (data && data.length > 0) {
        for (var i = 0; i < data.length; i++) {
          let row = data[i]
          const value = DatavChartsBuild.getTypeValue(
            row[options.yField.alias],
            options.yField.dbType
          )
          options.config.number.push(value)
        }
      }
      return options
    }
    return null
  },
  buildScrollBoard: (field, data) => {
    let options = field.options
    if (options.yField && options.yField.length > 0) {
      let configData = []
      if (data && data.length > 0) {
        for (var i = 0; i < data.length; i++) {
          let item = []
          let row = data[i]
          options.yField.forEach(f => {
            item.push(row[f.alias])
          })
          configData.push(item)
        }
        options.config.data = configData
      }
      return options
    }
    return null
  },
  buildDecoration: (field, data) => {
    let options = field.options
    if (options.xField && options.xField.alias && data) {
      let configData = []
      let value = ''
      if (data instanceof Array) {
        if (data && data.length > 0) {
          value = data[0][options.xField.alias]
        }
      } else if (data instanceof Object && data[options.xField.alias]) {
        value = data[options.xField.alias]
      } else {
        value = data
      }
      options.config.text.content = value
      return options
    }
    return null
  }
}

export default DatavChartsBuild
